﻿using Leeder.Logger;
using LeederGlobal;
using NewAndong.Business;
using NewAndong.Comm;
using NewAndong.Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace NewAndong.Forms
{
    public partial class FrmUnpackingSet : UIPage
    {
        bool isLoad = true;
        AutoSizeFormClass asc = new AutoSizeFormClass();
        public LogOperate LogObj => Log.GetLogger(this.GetType());
        UnpackingDirectionBLL bllUnpackingDirection = new UnpackingDirectionBLL();

        public FrmUnpackingSet()
        {
            InitializeComponent();
        }

        private void FrmUnpackingSet_Load(object sender, EventArgs e)
        {
            if (!isLoad)
                return;
            isLoad = false;

            Init_Control();
            RefreshDgv();
        }

        private void Init_Control()
        {
            //开捆
            dgv.AutoGenerateColumns = false;
            dgv.AllowUserToAddRows = false;
            dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgv.ReadOnly = true;
            //dgv.AddColumn("序号", "ID", 10);
            dgv.AddColumn("开捆名称", "UnpackingName", 30);
            //dgv.AddColumn("链数信号源", "SignalMark_LS", 30);
            //dgv.AddColumn("台数信号源", "SignalMark_TS", 30);
            uiPanel2.RectColor = Color.FromArgb(235, 243, 255);
        }

        /// <summary>
        /// 加载开捆设置
        /// </summary>
        private void RefreshDgv()
        {
            List<UnpackingDirectionModel> unpackingDirectionList = bllUnpackingDirection.GetModelList();
            dgv.DataSource = null;
            dgv.DataSource = unpackingDirectionList.Where(x => x.DataType == "链数").OrderBy(x => x.ID).ToList();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Edit();
            RefreshDgv();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 1)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            Edit(dgv.CurrentRow.DataBoundItem as UnpackingDirectionModel);
            RefreshDgv();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 1)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }

            if (UIMessageDialog.ShowMessageDialog("确定要删除该数据吗？", UILocalize.AskTitle, true, UIStyle.Red, true, false))
            {
                var model = dgv.CurrentRow.DataBoundItem as UnpackingDirectionModel;
                bllUnpackingDirection.Delete(model);
                RefreshDgv();
            }
        }

        private void dgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 1)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            var dgv = (UIDataGridView)sender;
            Edit(dgv.CurrentRow.DataBoundItem as UnpackingDirectionModel);
            RefreshDgv();
        }

        private void Edit(UnpackingDirectionModel model = null)
        {
            var triaConnects = GolbalVariable.TriaConnectList.Select(x => x.ID).ToArray();
            var triaGroups = GolbalVariable.TriaGroupList.Where(x => x.SignalMark == "PICS" && triaConnects.Contains(x.TriaConnectID)).Select(x => x.ID).ToArray();
            var signalMarkModelList = GolbalVariable.TriaItemList.Where(s => triaGroups.Contains(s.GroupID)).ToList();
            var signalMarks = GolbalVariable.TriaItemList.Where(s => triaGroups.Contains(s.GroupID)).Select(x => x.SignalMark).ToArray();

            UIEditOption option = new UIEditOption();
            option.AutoLabelWidth = true;
            if (model != null)
            {
                var modelTs = bllUnpackingDirection.GetModel("台数", model.UnpackingName);
                option.Text = "开捆设置";
                option.AddText("UnpackingName", "开捆名称", model.UnpackingName, true);
                option.AddCombobox("SignalMark_LS", "链数信号源", signalMarkModelList, "SignalMark", "SignalMark", model.SignalMark);
                if (modelTs != null)
                    option.AddCombobox("SignalMark_TS", "台数信号源", signalMarkModelList, "SignalMark", "SignalMark", modelTs.SignalMark);
                else
                    option.AddCombobox("SignalMark_TS", "台数信号源", signalMarks);
            }
            else
            {
                option.Text = "新增开捆";
                option.AddText("UnpackingName", "开捆名称", null, true);
                option.AddCombobox("SignalMark_LS", "链数信号源", signalMarks);
                option.AddCombobox("SignalMark_TS", "台数信号源", signalMarks);
            }
            UIEditForm frm = new UIEditForm(option);
            frm.ShowDialog();

            if (frm.IsOK)
            {
                if (model != null)
                {
                    var modelTs = bllUnpackingDirection.GetModel("台数", model.UnpackingName);

                    model.UnpackingName = frm["UnpackingName"].ToString();
                    model.SignalMark = frm["SignalMark_LS"].ToString();

                    if (modelTs != null)
                    {
                        modelTs.UnpackingName = frm["UnpackingName"].ToString();
                        modelTs.SignalMark = frm["SignalMark_TS"].ToString();
                        var result1 = bllUnpackingDirection.Update(modelTs);
                    }
                    else
                    {
                        modelTs = new UnpackingDirectionModel();
                        modelTs.UnpackingName = frm["UnpackingName"].ToString();
                        modelTs.SignalMark = frm["SignalMark_TS"].ToString();
                        modelTs.DataType = "台数";
                        var result1 = bllUnpackingDirection.Add(modelTs);
                    }
                    var result2 = bllUnpackingDirection.Update(model);
                    if (result2.IsSuccess)
                    {
                        UIMessageTip.ShowOk("修改成功");
                        LogObj.Info("修改出发链分组" + model.UnpackingName + "成功;作业者:" + ApplicationCache.CurrentUser.LoginName);
                    }
                    else
                    {
                        UIMessageBox.ShowError("修改失败！");
                        LogObj.Error("修改出发链分组" + model.UnpackingName + "失败;作业者:" + ApplicationCache.CurrentUser.LoginName);
                    }
                }
                else
                {
                    UnpackingDirectionModel modelLs = new UnpackingDirectionModel();
                    UnpackingDirectionModel modelTs = new UnpackingDirectionModel();
                    modelLs.UnpackingName = frm["UnpackingName"].ToString();
                    modelLs.SignalMark = signalMarks[int.Parse(frm["SignalMark_LS"].ToString())];
                    modelLs.DataType = "链数";
                    modelTs.UnpackingName = frm["UnpackingName"].ToString();
                    modelTs.SignalMark = signalMarks[int.Parse(frm["SignalMark_TS"].ToString())];
                    modelTs.DataType = "台数";

                    var result1 = bllUnpackingDirection.Add(modelLs);
                    var result2 = bllUnpackingDirection.Add(modelTs);

                    if (result1.IsSuccess && result2.IsSuccess)
                    {
                        UIMessageTip.ShowOk("新增成功");
                        LogObj.Info("新增出发链分组" + modelTs.UnpackingName + "成功;作业者:" + ApplicationCache.CurrentUser.LoginName);
                    }
                    else
                    {
                        UIMessageBox.ShowError("新增失败！");
                        LogObj.Error("新增出发链分组" + modelTs.UnpackingName + "失败;作业者:" + ApplicationCache.CurrentUser.LoginName);
                    }
                }
            }
            frm.Dispose();
        }
    }
}
